package com.stx.blog.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.stx.blog.dao.BlogCellectDao;
import com.stx.blog.entity.BlogCellectEntity;
import com.stx.blog.vo.BlogCellectVo;

public class BlogCellectDaoImpl extends JdbcDaoSupport implements
		BlogCellectDao {

	public List<BlogCellectEntity> list(BlogCellectVo vo) {

		String sql = "select * from BlogCellect";

		JdbcTemplate jt = getJdbcTemplate();

		List<BlogCellectEntity> blgcltList = jt.query(sql,
				new BlogCellectRowMapper());

		return blgcltList;
	}

	class BlogCellectRowMapper implements RowMapper {

		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

			BlogCellectEntity entity = new BlogCellectEntity();

			entity.setCno(rs.getInt("cno"));
			entity.setCtitle(rs.getString("ctitle"));
			entity.setCcontent(rs.getString("ccontent"));

			return entity;

		}
	}

	public void delBlogCellect(String cno) {

		JdbcTemplate jt = getJdbcTemplate();

		String sql = "delete from BlogCellect where cno = ?";

		// 构建参数数组
		Object[] args = { cno };
		jt.update(sql, args);
	}

	public void addBlogCellect(BlogCellectVo BlogCellect) {

		JdbcTemplate jt = getJdbcTemplate();

		String sql = "insert into BlogCellect(cno,ctitle,ccontent,ccreatetime) values(blogCellect_seq.nextval,?,?,?)";

		// 构建参数数组
		Object[] args = { BlogCellect.getCno(), BlogCellect.getCtitle(),
				BlogCellect.getCcontent(), BlogCellect.getCcreatetime() };

		jt.update(sql, args);
	}

	public BlogCellectEntity findBlogCellectBySno(String cno) {

		JdbcTemplate jt = getJdbcTemplate();

		String sql = "select * from BlogCellect where cno = ?";

		// 构建参数数组
		Object[] args = { cno };

		BlogCellectEntity blogCellectEntity = (BlogCellectEntity) jt
				.queryForObject(sql, args, new BlogCellectRowMapper());

		return blogCellectEntity;

	}


}
